import requests
import spider
import urllib
from spider import common

class TypeSpider():
    def __init__(self):
        self.url = "https://h5.ele.me/restapi/shopping/v2/entries?latitude=22.54286&longitude=114.059563&templates[]=main_template&templates[]=favourable_template&templates[]=svip_template&terminal=h5"

    def run(self):
        res =  requests.get(self.url,headers=spider.headers)
        res_data = res.json()
        #print(res_data)
        self.dataParse(res_data[0])

    def dataParse(self,type_data):
        data_list = type_data.get("entries",[])
        all_data = []
        for data in data_list:
            image_hash = data.get("image_hash","")
            imge_url = common.img_handle_elm(image_hash)+"?imageMogr/format/webp/thumbnail/!90x90r/gravity/Center/crop/90x90/"
            name = data.get("name","")
            id = data.get("id", "")
            activity_id = data.get("activity_id", "")
            result = {
                "img":imge_url,
                "type_id":str(id),
                "active_id":str(activity_id),
                "name":name
            }
            all_data.append(result)
        spider.insert_list(all_data,"t_type")
